package com.egg.ylt.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.RecyclerViewUtil;
import com.egg.ylt.Utils.StringUtil;
import com.egg.ylt.activity.ACT_NewsDetails;
import com.egg.ylt.activity.ACT_VideoPlaying;
import com.egg.ylt.adapter.ADA_ConsultRecyclerView;
import com.egg.ylt.pojo.HomePageEntity;
import com.egg.ylt.presenter.impl.CollectNewsPresenterImpl;
import com.egg.ylt.view.ICollectNewsView;
import com.yonyou.framework.library.base.BaseFragment;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.eventbus.EventCenter;

import butterknife.BindView;

/**
 * sichaoqi on 2018/12/14
 */
public class FRA_CollectNews extends BaseFragment<CollectNewsPresenterImpl> implements ICollectNewsView {

    @BindView(R.id.no_data_ll)
    LinearLayout mNoDataLl;
    @BindView(R.id.collect_recycler_news)
    RecyclerView collectRecyclerNews;
    @BindView(R.id.rl_loading_view)
    RelativeLayout rlLoadingView;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefreshLayout;

    private ADA_ConsultRecyclerView mNewsAdapter;

    private String pageSize = "20";
    private int pageNum = 1;
    private int listSize = 0;
    //下拉刷新 和 上拉加载状态值
    private int mRefreshStateInt = 0;
    private RecyclerViewUtil mRecyclerViewUtil;

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {
        if (mPresenter != null) {
            //请求门店展示数据
            mRefreshStateInt = 0;
            pageNum = 1;
            //资讯信息
            mPresenter.getNewsData(Constants.TOKEN, Constants.COMPANYID, "0", pageSize, String.valueOf(pageNum));
        }
    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected View getLoadingTargetView() {
        return rlLoadingView;
    }


    @Override
    protected void initViewsAndEvents() {

        //初始化recycler
        initRecycler();

        //下拉刷新  上拉加载
        initRefresh();
    }

    private void initRecycler() {
        //设置布局管理器
        collectRecyclerNews.setLayoutManager(new LinearLayoutManager(mContext));

        //设置适配器
        mNewsAdapter = new ADA_ConsultRecyclerView(mContext);
        collectRecyclerNews.setAdapter(mNewsAdapter);

        //list的条目监听
        mNewsAdapter.setOnItemClickListener(new ADA_ConsultRecyclerView.OnItemClickListener() {
            @Override
            public void onItemClick(String id, String type) {
                Bundle mBundle = new Bundle();
                mBundle.putString("id", id);
                switch (type) {
                    case "0":
                    case "1":
                    case "2":
                        readyGo(ACT_NewsDetails.class, mBundle);
                        break;
                    case "3":
                        readyGo(ACT_VideoPlaying.class, mBundle);
                        break;
                }
            }
        });

    }

    private void initRefresh() {
        //下拉刷新的设置
        swipeRefreshLayout.setColorScheme(R.color.color_FE752D);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //请求门店展示数据
                mRefreshStateInt = 0;
                pageNum = 1;
                //资讯信息
                mPresenter.getNewsData(Constants.TOKEN, Constants.COMPANYID, "0", pageSize, String.valueOf(pageNum));
            }
        });

        //设置上拉加载更多
        mRecyclerViewUtil = new RecyclerViewUtil(collectRecyclerNews);
        mRecyclerViewUtil.setRecyclerViewLoadMoreListener(new RecyclerViewUtil.RecyclerViewLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (listSize == Integer.parseInt(pageSize)) {
                    mRefreshStateInt = 1;
                    pageNum++;
                    mRecyclerViewUtil.setLoadMoreEnable(false);
                    //资讯信息
                    mPresenter.getNewsData(Constants.TOKEN, Constants.COMPANYID, "0", pageSize,
                            String.valueOf(pageNum));
                }

            }
        });

        //请求收藏咨询列表数据  0是咨询  1 门店
        mPresenter.getNewsData(Constants.TOKEN, Constants.COMPANYID, "0", pageSize, String.valueOf(pageNum));
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fra_collect_news;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    public void getNewsData(HomePageEntity entity) {
        //得到资讯数据
        if (pageNum == 1 && ListUtil.isListEmpty(entity.getList())) {
            collectRecyclerNews.setVisibility(View.GONE);
            mNoDataLl.setVisibility(View.VISIBLE);
            return;
        }
        //设置咨询数据展示
        listSize = entity.getList().size();

        switch (mRefreshStateInt) {
            case 0:
                mNewsAdapter.addList(entity.getList());
                //在onRefresh 里调用获取数据的方法就好了 数据获取完毕 别忘了 修改状态
                swipeRefreshLayout.setRefreshing(false);
                break;
            case 1:
                mNewsAdapter.updateList(entity.getList());
                //加载完数据后回复上拉状态
                mRecyclerViewUtil.setLoadMoreEnable(true);
                break;
        }
    }
}
